import tools from '@/common/tools'
import * as api from '@/api/index'
import cache from '@/common/cache'
const LOGIN     = 'LOGIN'
const SET_TOKEN = 'SET_TOKEN'
const SET_USER  = 'SET_USER'
const LOGOUT    = 'LOGOUT'
export default {
  namespaced: true,
  state: {
    user: '',
    token: tools.getToken()
  },
  mutations: {
    [SET_TOKEN] (state, data) {
      state.token = data
      cache.put('token',data);
    },

    [SET_USER] (state, data) {
      state.user = data
      cache.put('user', data)
    },
    [LOGOUT] (state, data) {
      state.user = null
      state.token = null
      cache.remove('token')
      cache.remove('user')
    }

  },
  actions: {

    login (context, data) {
      return new Promise((resolve, reject) => {
        api.login(data.data).then((res) => {
          context.commit(SET_USER, res.data.user)
          context.commit(SET_TOKEN, res.data.token)
          resolve(true)
        }).catch((error) => {
          reject(error)
        })
      })
    },
    //openid自动登录
    /*autologin(context, data){
      return new Promise(resolve => {

          console.log('openid自动登录')
          console.log(data.user)
          console.log(data.token)
          context.commit(SET_USER, data.user)
          context.commit(SET_TOKEN,data.token)
          resolve(data)
      })
    },*/
    //授权成功授权
    authorize(context,res){
      return new Promise(resolve => {
          context.commit(SET_USER, res.data.userInfo)
          context.commit(SET_TOKEN, res.data.token)
           resolve(res.data)
      })

    },

    //授权成功授权
    bindPhone(context,res){
      return new Promise(resolve => {
          context.commit(SET_USER, res.data.userinfo)
          resolve(res.data)
      })
    },
    bind ({ commit }, data) {
      return new Promise((resolve, reject) => {
        bind(data).then((res) => {
          commit(SET_USER, res.data.user)
          commit(SET_TOKEN, res.data.token+'bbbbb')
          resolve(true)
        }).catch((error) => {
          reject(error)
        })
      })
    },
    disbindwechat ({ commit }) {
      return new Promise((resolve, reject) => {
        disbindwechat().then((res) => {
          commit(SET_USER, res.data.user)
          resolve(true)
        })
      })
    },
    // get user info
    getInfo ({commit,state}, data={forceUpdate:false}) {
      return new Promise((resolve, reject) => {

        console.log('getInfo')
        console.log(data)
        //非强制更新拿用户信息
        if(state.user && !data.forceUpdate){
          resolve(state.user);
        }else if(tools.getToken() && (data.forceUpdate || !state.user)){
            api.userInfo().then(res => {
              const { data } = res
              if (!data) {
                resolve(false)
              } else {
                console.log('data')
                console.log(data)
                console.log(res)
               // commit(SET_TOKEN, state.token+'2313213213123123')
                commit(SET_USER, res.data)
                resolve(data)
              }
            }).catch(error => {
              reject(error)
            })
        }
      })
    },
    // 修改资料
    profile ({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        api.userProfile(data).then(res => {
          commit(SET_USER, res.data)
          resolve(res.data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 退出
    logout (context, payload) {
      return new Promise((resolve, reject) => {
        context.commit(LOGOUT, payload)
        resolve(true)
      })
    }
  }
}



